package com.xvwang.blog.service;

import com.xvwang.blog.dao.UserDao;
import com.xvwang.blog.model.User;
import org.mindrot.jbcrypt.BCrypt;

public class UserService {
    private final UserDao userDao = new UserDao();

    public User register(String username,String nickname,String password,String avatar,String git) {
        String salt = BCrypt.gensalt();
        String hashpw = BCrypt.hashpw(password,salt);

        return userDao.insert(username,nickname,hashpw,avatar,git);
    }

    public User login(String username, String password) {
        //获取登录用户信息
        User user = UserDao.selectOneByUsername(username);

        //验证登录用户名密码是否正确
        if (user == null) {
            return null;
        }
        if (BCrypt.checkpw(password, user.password)) {
            return user;
        }
        return null;
    }
}

